//#include "layout-lan-setting.h"
#include "sms.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libintl.h>
#include <locale.h>
#include <signal.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <linux/if.h>
#include <arpa/inet.h>

#define DEBUG 1
#ifdef DEBUG
#define DBG(txt, args... ) fprintf(stderr, "WIRELESS-LAN-DEBUG: " txt , ##args )
#else
#define DBG(txt, args... ) /* nothing */
#endif


#define STATUS_FILE             "/etc/network/lan.status"
static gboolean RUNNING;
static gboolean DYNAMIC_CONNECT_RUN;

static void lan_read_interface_info(Iface_t *iface);
static void lan_save_nameserver(Layout * layout);
void get_name_server(const gchar *file, Layout * layout);
static gboolean lan_address_is_valid(gchar *ip);

static gboolean lan_setting_layout_load_bg(xmlNode *a_node,Layout *layout)
{
  LanLayout *lanlayout=layout->private;
  lanlayout->bg = (BackGround*)g_malloc0 (sizeof(BackGround)+1);
  GdkPixbuf*  pixbuf;
  xmlChar * val;
  char pic[256];

  if(lanlayout->bg->bg|| !a_node)
  {
    return FALSE;
  }

  val = xmlGetProp (a_node,(xmlChar*)"pic");
  if(!val)
  {
    return FALSE;
  }

  sprintf(pic,"%s/%s/layout/%s/%s",SMS_RESOURCE_DIR,"default"/*theme*/,layout->name,val);
  printf("layout->name=%s\n" ,layout->name);
  pixbuf=sms_load_pic(pic,800,480);
  if(!pixbuf)
  {
    return FALSE;
  }

  lanlayout->bg->bg=goo_canvas_image_new (layout->cavas, pixbuf, 0, 0,NULL);
  g_object_unref(pixbuf);

  val = xmlGetProp (a_node,(xmlChar*)"pic_1");
  printf("@@@@val=%s\n" ,val);
  if(!val)
  {
    return FALSE;
  }	
  sprintf(pic,"%s/%s/layout/%s/%s",SMS_RESOURCE_DIR,"default"/*theme*/,layout->name,val);
  printf("@@@@@@@@@layout->name=%s\n" ,layout->name);
  pixbuf=sms_load_pic(pic,550,530);
  if(!pixbuf)
  {
    return FALSE;
  }	
  lanlayout->bg->pic1 = goo_canvas_image_new(layout->cavas ,pixbuf ,250 ,0 ,NULL);
  g_object_unref(pixbuf);

  /********LABEL AREA*************/
  lanlayout->bg->label = goo_canvas_text_new(layout->cavas,"",400,65,100,
      GTK_ANCHOR_WEST,"fill-color", "red","font", "Sans Bold 20",NULL);

  return TRUE;
}

static void  lan_setting_layout_create_icon(xmlNode *a_node,Layout *layout ,int id)
{
  LanLayout *lanlayout=layout->private;
  SMS *sms=layout->sms;
  GdkPixbuf*  pixbuf;
  xmlChar * val;
  char pic[256];

  printf("id=%d\n" ,id);
  val = xmlGetProp (a_node,(xmlChar*)"name");
  if(!val)
  {
    goto err;
  }
  lanlayout->icons[id].name=g_strdup(val);
  lanlayout->icons[id].size=sms_get_xml_int(a_node,(xmlChar*)"size",64);
  lanlayout->icons[id].asize=sms_get_xml_int(a_node,(xmlChar*)"asize",lanlayout->icons[id].size);     
  lanlayout->icons[id].x=sms_get_xml_int(a_node,(xmlChar*)"x",lanlayout->icons[id].x);     
  lanlayout->icons[id].y=sms_get_xml_int(a_node,(xmlChar*)"y",lanlayout->icons[id].y);   

  /************icon creat****************/
  val = xmlGetProp (a_node,(xmlChar*)"pic");
  if(!val)
  {
    goto err;
  }

  sprintf(pic,"%s/%s/layout/%s/%s",SMS_RESOURCE_DIR,"default"/*theme*/,layout->name,val);
  pixbuf=sms_load_pic(pic,lanlayout->icons[id].size,lanlayout->icons[id].size);
  if(!pixbuf)
  {
    goto err;
  }

  lanlayout->icons[id].icon=goo_canvas_image_new (layout->cavas, pixbuf, 0, 0 ,"can-focus", FALSE,NULL);	
  g_object_set(lanlayout->icons[id].icon,"visibility",GOO_CANVAS_ITEM_HIDDEN,NULL);

  /*************icon ani******************/
  val = xmlGetProp (a_node,(xmlChar*)"apic");
  if(!val)
  {
    goto err;
  }

  sprintf(pic,"%s/%s/layout/%s/%s",SMS_RESOURCE_DIR,"default"/*theme*/,layout->name,val);
  pixbuf=sms_load_pic(pic,lanlayout->icons[id].asize,lanlayout->icons[id].asize);
  if(!pixbuf)
  {
    goto err;
  }

  lanlayout->icons[id].icon_a=goo_canvas_image_new (layout->cavas, pixbuf, 0, 0,"can-focus", FALSE , NULL);
  g_object_set(lanlayout->icons[id].icon_a,"visibility",GOO_CANVAS_ITEM_VISIBLE,NULL);

#if 0
  /**************is layout ?*************/
  val = xmlGetProp (a_node,(xmlChar*)"layout");
  if(val)
  {
    id=sms_get_win_id(sms,val);
    if(id<0)
      goto err;
    icon->id=id;
    icon->type=MAIN_ICON_TYPE_LAYOUT;

  }
#endif
  return ;

err:
  printf("fdfdfdf\n");
  fprintf(stderr,"%s:error\n",__FUNCTION__);
  //return ;
}

static gboolean lan_setting_layout_load_icons(xmlNode *a_node,Layout *layout)
{
  LanLayout *lanlayout=layout->private;
  xmlNode *cur_node = NULL;
  int i=0;
  IconSet * icon;

  if(!a_node)
  {
    return FALSE;
  }

  for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
    if (cur_node->type == XML_ELEMENT_NODE){
      if(strcmp(cur_node->name,"icon")==0){
        lan_setting_layout_create_icon(cur_node,layout ,i);
        i++;
      }
    }
  }
  printf("i = %d \n" ,i);
  lanlayout->micon_count = i ;

  return TRUE ;

}

static Entry *create_editable_entry(xmlNode *a_node ,Layout *layout)
{
  printf("~~~%s\n" ,__FUNCTION__);
  LanLayout *lanlayout=layout->private;
  SMS *sms=layout->sms;
  Entry *entry = (Entry *)g_malloc0 (sizeof(Entry)+1);
  //GtkWidget *entry;
  GtkTooltips *tooltips ;
  xmlChar * val;

  val = xmlGetProp (a_node,(xmlChar*)"name");
  if(!val)
    goto err;
  entry->name = g_strdup(val); 

  val = xmlGetProp (a_node,(xmlChar*)"tooltips");
  if(!val)
    goto err;
  entry->tooltips = g_strdup(val); 

  val = xmlGetProp (a_node,(xmlChar*)"font");
  if(!val)
    goto err;
  entry->font = g_strdup(val); 

  entry->entry_x = sms_get_xml_int(a_node,(xmlChar*)"entry_x",entry->entry_x);  
  entry->entry_y = sms_get_xml_int(a_node,(xmlChar*)"entry_y",entry->entry_y);  
  entry->text_x = sms_get_xml_int(a_node,(xmlChar*)"text_x",entry->text_x);  
  entry->text_y = sms_get_xml_int(a_node,(xmlChar*)"text_y",entry->text_y);  
  printf("entry_x =%d ,entry_y=%d ,text_x=%d ,text_y=%d\n" ,
      entry->entry_x , entry->entry_y ,
      entry->text_x ,entry->text_y);

  entry->text = goo_canvas_text_new(layout->cavas ,entry->name ,
      entry->text_x ,entry->text_y ,-1 ,
      GTK_ANCHOR_W ,"font" ,entry->font ,NULL) ;
  g_object_set (entry->text ,"fill-color" ,"black" ,NULL);

  tooltips = gtk_tooltips_new();
  entry->entry_lan = gtk_entry_new();
  printf("@@@%p\n" ,entry->entry_lan);
  gtk_editable_set_editable(GTK_EDITABLE(entry->entry_lan) ,TRUE);
  gtk_tooltips_set_tip(tooltips, entry->entry_lan, entry->tooltips, NULL);
  printf("entry->name = %s\n" ,entry->name);
  g_object_set_data(G_OBJECT(sms->window), entry->name, entry->entry_lan);
  goo_canvas_widget_new(layout->cavas ,entry->entry_lan ,
      entry->entry_x ,entry->entry_y ,
      -1 ,-1 ,NULL);

  return entry;
err:
  fprintf(stderr,"%s:error\n",__FUNCTION__);
  //lan_entry_destroy(lanlayout->entry);
  return ;

}

static gboolean lan_setting_layout_load_widget(xmlNode *a_node ,Layout *layout)
{
  printf("~~~%s\n" ,__FUNCTION__);
  LanLayout *lanlayout=layout->private;
  xmlNode *cur_node = NULL;
  Entry *entry ;
  int i = 0;
  if(!a_node)
    return FALSE;

  for (cur_node = a_node; cur_node; cur_node = cur_node->next)
  {
    if (cur_node->type == XML_ELEMENT_NODE)
    {
      if(strcmp(cur_node->name,"entry")==0)
      {
        entry = create_editable_entry(cur_node , layout);
        printf("entry =%p\n" ,entry);
        /*
           if(i == 3){
           layout->widget = entry ;
           }
           */
        if(entry)
        {
          lanlayout->entry_count++ ;
          if(lanlayout->entrys)
          {
            Entry *pentry ;
            pentry = lanlayout->entrys->prv ;
            pentry->next = entry ;
            entry->prv = pentry ;

            lanlayout->entrys->prv = entry ;
            entry->next = lanlayout->entrys ;
          }
          else
          {
            lanlayout->entrys = entry ;
            entry->next = entry ;
            entry->prv = entry ;
          }
        }
      }
    }
    //i++;
  }

  return TRUE ;
}

static gboolean lan_setting_layout_travel_xml(xmlNode *a_node,Layout *layout)
{
  printf("~~~~%s\n" ,__FUNCTION__);
  LanLayout *lanlayout=layout->private;
  xmlNode *cur_node = NULL;
  xmlNode *bg_node = NULL;
  xmlNode *icon_node = NULL;
  xmlNode *entry_node = NULL;

  for (cur_node = a_node; cur_node; cur_node = cur_node->next) 
  {
    if (cur_node->type == XML_ELEMENT_NODE)
    {
      printf("node type: Element, name: %s\n", cur_node->name);
      if(strcmp(cur_node->name,"bg")==0)
        bg_node=cur_node;
      else if(strcmp(cur_node->name,"icons")==0)
        icon_node=cur_node;
      else if(strcmp(cur_node->name,"entrys")==0)
        entry_node=cur_node;
    }
  }

  if(!bg_node||!icon_node || !entry_node)
  {
    return FALSE;
  }

  lan_setting_layout_load_bg(bg_node,layout);
  lan_setting_layout_load_icons(icon_node->children,layout);
  lan_setting_layout_load_widget(entry_node->children ,layout);

  if(lanlayout->micon_count>=2)
  {
    return TRUE;
  }

  return FALSE;

}

gboolean lan_setting_layout_load_config(Layout *layout)
{
  gboolean ret=FALSE;
  xmlDoc *doc = NULL;
  xmlNode *root_element = NULL;
  LanLayout *lanlayout = layout->private ;
  char conf[256];
  sprintf(conf,"%s/%s/layout/%s/layout.xml",SMS_RESOURCE_DIR,"default"/*theme*/,layout->name);
  printf("layout->name=%s\n" ,layout->name);

  doc = xmlReadFile(conf, NULL, 0);
  if (doc == NULL) {
    fprintf(stderr,"error: could not parse file %s\n", conf);
    return ret;
  }
  root_element = xmlDocGetRootElement(doc);

  if(root_element)
  {
    xmlNode *cur_node = NULL;
    for (cur_node = root_element; cur_node; cur_node = cur_node->next) {
      if (cur_node->type == XML_ELEMENT_NODE) {
        ret=TRUE;
        break;
      }
    }
    if(ret)
      ret=lan_setting_layout_travel_xml(cur_node->children,layout);
  }

  xmlFreeDoc(doc);
  xmlCleanupParser();
  return ret;

}

void lan_setting_layout_reset_icons(Layout *layout)
{
  LanLayout *lanlayout=layout->private;
  IconSet * icon=lanlayout->icons;
  int i  ;
  //test_layout->focus_id=0;

  for (i = 0 ; i < lanlayout->micon_count ; i++)
  {
    g_object_set(lanlayout->icons[i].icon,"visibility",GOO_CANVAS_ITEM_VISIBLE,NULL);
    g_object_set(lanlayout->icons[i].icon_a,"visibility",GOO_CANVAS_ITEM_HIDDEN,NULL);
    goo_canvas_item_translate(lanlayout->icons[i].icon,
        lanlayout->icons[i].x,lanlayout->icons[i].y);    
    goo_canvas_item_translate(lanlayout->icons[i].icon_a,
        lanlayout->icons[i].x,lanlayout->icons[i].y);    
  }
  g_object_set(lanlayout->icons[0].icon,"visibility",GOO_CANVAS_ITEM_HIDDEN,NULL);
  g_object_set(lanlayout->icons[0].icon_a,"visibility",GOO_CANVAS_ITEM_VISIBLE,NULL);


}

static void set_focus_netmask(GtkEntry *widget, gpointer data)
{
  Layout *layout = (Layout *)data ;
  LanLayout *lanlayout = layout->private;
  //Entry *entry = layout->widget ;
  SMS *sms = layout->sms ;

  //entry->entry_lan = g_object_get_data(G_OBJECT(sms->window), entry->name);
  //gtk_window_set_focus(GTK_WINDOW(sms->window), entry->entry_lan);

  //LanLayout *lanlayout=layout->private;
  //Entry *entry = (Entry *)data;
  //char *buf = (char *)data ;

  //entry = entry->next ;
  //printf("entry name = %s \n" ,entry->name);
  //gtk_window_set_focus(GTK_WINDOW(sms->window), entry->entry_lan);
}

void lan_setting_layout_reset_widget(Layout *layout)
{
  printf("~~~%s\n" ,__FUNCTION__);
  LanLayout *lanlayout=layout->private ;
  SMS *sms = layout->sms ;
  Entry *entry = lanlayout->entrys ;
  int i ;

  memset(&(lanlayout->iface), 0, sizeof(Iface_t));
  strcpy(lanlayout->iface.name , "eth0");
  printf("---begain read interface info----\n");
  lan_read_interface_info(&(lanlayout->iface));
  lanlayout->iface.isdhcp = FALSE ;
  if ((lanlayout->iface.nameserver[0] == NULL) && (lanlayout->iface.nameserver[1] == NULL))
  {
    gchar *nameserverfile = NULL;
    nameserverfile = g_strdup("/etc/resolv.conf");
    get_name_server(nameserverfile, layout);
    if (!lanlayout->iface.nameserver[0]) {
      lanlayout->iface.nameserver[0] = g_strdup("");
    }
    if (lanlayout->iface.nameserver[1]) {
    	printf("~~~%s\n" ,__FUNCTION__);
      lanlayout->iface.nameserver[1] = g_strdup("");					
    }
  }

  printf("~~%p ,%p \n" ,lanlayout->iface.nameserver[0] ,lanlayout->iface.nameserver[1]);
  

  for(i = 0 ; i < lanlayout->entry_count ; i++)
  {
    printf("entry name = %s \n" ,entry->name);
    entry->entry_lan = g_object_get_data(G_OBJECT(sms->window), entry->name);
    if(i == 0){
			gtk_entry_set_text(GTK_ENTRY(entry->entry_lan), lanlayout->iface.address);
    }
    else if(i == 1){
			gtk_entry_set_text(GTK_ENTRY(entry->entry_lan), lanlayout->iface.netmask);
    }
    else if(i == 2){
			gtk_entry_set_text(GTK_ENTRY(entry->entry_lan), lanlayout->iface.gateway);
    }
    else if(i == 3){
			gtk_entry_set_text(GTK_ENTRY(entry->entry_lan), lanlayout->iface.nameserver[0]);
    }
    else if(i == 4){
			gtk_entry_set_text(GTK_ENTRY(entry->entry_lan), lanlayout->iface.nameserver[1]);
    }
    g_signal_connect(G_OBJECT(entry->entry_lan), "activate", G_CALLBACK(set_focus_netmask), layout);
    if(i < lanlayout->entry_count-1)
    {
      entry = entry->next ;
      //layout->widget = entry ;
      printf("!!!!!next@@@%p\n" ,entry->entry_lan);
    }
  }
}

static gboolean lan_setting_layout_init(Layout *layout,gpointer data)
{
	printf("~~~%s\n" ,__FUNCTION__);
  LanLayout *lanlayout = (LanLayout *)g_malloc0(sizeof (LanLayout)+1);
  //lanlayout->focus_id = -1 ;
  layout->private = (void *)lanlayout ;
  layout->cavas = goo_canvas_group_new(layout->parent,NULL);	

  

  if(!lan_setting_layout_load_config(layout))
  {
    fprintf(stderr,"%s:load config err\n",__FUNCTION__);
    //return FALSE;//exit(-1);
  }
  lan_setting_layout_reset_icons(layout);
  lan_setting_layout_reset_widget(layout);

  return TRUE;
}

static gboolean lan_setting_layout_clear(Layout *layout,gpointer data)
{
  LanLayout *lanlayout ;
  int i;

  printf("call %s\n",__FUNCTION__);

  lanlayout=layout->private;

  g_object_unref(lanlayout->bg);	
  g_object_unref(lanlayout->bg->bg);
  g_object_unref(lanlayout->bg->pic1);	

  g_free(lanlayout);
}

static void change_icon(Layout *layout, int icon_id)
{
  LanLayout *lanlayout = layout->private;
  int i = 0;
  lanlayout->focus_id = icon_id ;
  printf("micon_count = %d ,icon_id=%d\n" ,lanlayout->micon_count ,icon_id);
  for (i  ; i < lanlayout->micon_count ; i++)
  {
    if(i == icon_id)
    {
      printf("i= %d ,icon_id=%d\n" ,i ,icon_id);
      g_object_set(lanlayout->icons[i].icon,"visibility",GOO_CANVAS_ITEM_HIDDEN,NULL);
      g_object_set(lanlayout->icons[i].icon_a,"visibility",GOO_CANVAS_ITEM_VISIBLE,NULL);
      printf("name = %s\n" ,lanlayout->icons[i].name);
    }
    else if(i != icon_id)
    {
      printf("!~i= %d ,icon_id=%d\n" ,i ,icon_id);
      g_object_set(lanlayout->icons[i].icon,"visibility",GOO_CANVAS_ITEM_VISIBLE,NULL);
      g_object_set(lanlayout->icons[i].icon_a,"visibility",GOO_CANVAS_ITEM_HIDDEN,NULL);
      printf("name = %s\n" ,lanlayout->icons[i].name);
    }
  }
}

static void lan_setting_layout_move_icon(Layout *layout,gboolean is_up)
{
  LanLayout *lanlayout = layout->private;
  SMS *sms = layout->sms ;
  if(is_up)
  {
    change_icon(layout ,abs((lanset_focus_id)++)%(lanlayout->micon_count));
    sms->busy = FALSE ;
  }
  else
  {
    change_icon(layout ,abs((lanset_focus_id)--)%(lanlayout->micon_count));
    sms->busy = FALSE ;
  }
}

static gboolean dynamic_connect_style(Layout * layout)
{
  LanLayout *lanlayout = layout->private;
  SMS *sms = layout->sms ;
  static int count=0;
  gchar buf[25];

    if(!DYNAMIC_CONNECT_RUN)
    {
        printf("%s return\n", __func__);
        return FALSE;
    }
    
  if(count%3 == 0)
  {
    sprintf(buf,"connecting...");
    g_object_set(lanlayout->bg->label,"text",buf,NULL);
    g_object_set(lanlayout->bg->label,"fill-color","red",NULL);
  }
  else if(count%3 == 1)
  {
    sprintf(buf,"connecting..");
    g_object_set(lanlayout->bg->label,"text",buf,NULL);
    g_object_set(lanlayout->bg->label,"fill-color","red",NULL);
  }
  else 
  {
    sprintf(buf,"connecting.");
    g_object_set(lanlayout->bg->label,"text",buf,NULL);
    g_object_set(lanlayout->bg->label,"fill-color","red",NULL);			
  }
  count++ ;
  printf("count =%d\n" ,count);
  sms->busy = TRUE ;
  return TRUE ;
}

void hide_entrys(Layout *layout)
{
  printf("~~~%s\n" ,__FUNCTION__);
  lan_is_show = LAN_LAYOUT_SHOW;
  LanLayout *lanlayout = layout->private ;
  Entry *entry = lanlayout->entrys ;
  printf("entry =%p\n" ,entry);

  gtk_widget_hide(entry->entry_lan);

  entry = entry->next ;
  gtk_widget_hide(entry->entry_lan);
  printf("entry =%p\n" ,entry);

  entry = entry->next ;
  gtk_widget_hide(entry->entry_lan);
  printf("entry =%p\n" ,entry);

  entry = entry->next ;
  gtk_widget_hide(entry->entry_lan);
  printf("entry =%p\n" ,entry);

  entry = entry->next ;
  gtk_widget_hide(entry->entry_lan);
  printf("entry =%p\n" ,entry);

}

static gint get_first_char(gchar *s)
{
  printf("~~~~%s\n" ,__FUNCTION__);
  gint i = 0;

  if (s == NULL)
  {
    return -1;
  }
  for (i=0; i<strlen(s); i++)
    if ((s[i] != ' ') && (s[i] != '\t') && (s[i] != '\n') && (s[i] != '\0')) return i;
  return -1;
}

static gint get_param_val(gchar* line, gchar* param, gchar* value)
{
  printf("~~~~%s\n" ,__FUNCTION__);
  gint a = 0;
  gint b = 0;
  gint st = 0;
  gint sep = 0;

  st = get_first_char(line);
  if (line[st] == '#') return -1; // mark comment 
  for (sep=st; sep<strlen(line); sep++)
    if (line[sep]==' ') break;
  for (a=sep; a<strlen(line); a++)
    if ((line[a]!=' ') && (line[a]!='\t')) break;
  for (b=a+1; b<strlen(line); b++)
    if ((line[b]=='\n') || (line[b]=='\0')) break;
  param = strncpy(param,&line[st],sep-st);
  param[sep-st] = '\0';

  if ((b-a) > 0) value = strncpy(value,&line[a],b-a);	
  value[b-a]='\0';
  return 0;
}

static void lan_read_interface_info(Iface_t *iface)
{
  printf("~~~~%s\n" ,__FUNCTION__);
  gchar *filename;
  FILE *fd;
  char buffer[256] = {0};
  gchar paramval[256];
  gchar ifname[256];
  gchar *ret;
  gint index = 0;

  filename = g_strdup_printf("%sif_%s", NET_CONFIG_FOLDER, iface->name);    
  if (!access(filename, 4))
  {
    printf("Access file success:%s\n", filename);
    fd = fopen(filename, "r");
    if (fd)
    {
      do
      {
        ret = fgets(buffer, 255, fd);
        //printf("get from file: %s\n", buffer);
        if (get_param_val(buffer, paramval, ifname) == -1) 
        {
          printf("call %s failed!\n");
          memset(buffer, 0, 256);
          continue;  
        }
        if (!strcmp("iface", paramval))
        {
          if (!strcmp(paramval, iface->name))
          {
            printf("Wrong config file.\n");
            fclose(fd);
            return;
          }
        }
        else if (!strcmp("address", paramval))
        {
          strcpy(iface->address, g_strstrip(ifname));
        }
        else if (!strcmp("netmask", paramval))
        {
          strcpy(iface->netmask,g_strstrip(ifname));
        }
        else if(!strcmp("gateway", paramval))
        {
          strcpy(iface->gateway, g_strstrip(ifname));
        }
        else if(!strcmp("nameserver", paramval))
        {
          iface->nameserver[index] = g_strdup(ifname);
          //printf("****get nameserver %d: %s\n", index, iface->nameserver[index]);
          index++;
        }
        memset(buffer, 0, 256);
      }while(ret!=NULL);
      fclose(fd);
    }
  }
}


static void lan_wirte_interface_info(Iface_t *iface)
{
  printf("~~~%s\n" ,__FUNCTION__);
  FILE *fd = NULL;
  gchar *ret = NULL;
  gchar *filename = NULL;
  gchar ifname[256] = {0};
  gchar buffer[256] = {0};
  gchar paramval[256] = {0};

  // check param
  if (NULL == iface)
  {
    fprintf(stderr, "input param invalid in %s\n", __func__);
    return;
  }

  filename = g_strdup_printf("%sif_%s", NET_CONFIG_FOLDER, iface->name);    
  printf("name = %s\n" ,iface->name);
  remove(filename);
  fd = fopen(filename, "w");
  if (fd)
  {
    if (iface->name)
    {
      fprintf(fd, "iface %s\n", iface->name);
    }      
    if (!iface->isdhcp)
    {
      fprintf(fd, "addrtype static\n");
    }
    else
    {
      fprintf(fd, "addrtype dhcp\n");
    }        
    if (iface->address)
    {
      fprintf(fd, "address %s\n", iface->address);
    }
    if (iface->netmask)
    {
      fprintf(fd, "netmask %s\n", iface->netmask);
    }
    if (iface->gateway)
    {
      fprintf(fd, "gateway %s\n", iface->gateway);
    }
    if (iface->nameserver[0])
    {
      fprintf(fd, "nameserver %s\n", iface->nameserver[0]);
    }
    else
    {
      fprintf(fd, "nameserver \n");
    }
    if (iface->nameserver[1])
    {
      fprintf(fd, "nameserver %s\n", iface->nameserver[1]);
    }
  }
  fclose(fd);	
}

static void sig_action (int signo)
{
  FILE *fd = NULL;

  RUNNING=FALSE;
  DYNAMIC_CONNECT_RUN=FALSE;
}

static gboolean do_detect(Layout * layout)
{
  LanLayout *lanlayout = layout->private;
  SMS *sms = layout->sms ;
  gchar buffer[256];
  FILE *fd;
  int err_num;


  if(RUNNING){
    return TRUE;
  }

  fd=fopen(STATUS_FILE, "r");
  if (!fd)
  {
    printf("Error:lan.status file not found!\n");
    return FALSE;
  }

  fscanf(fd, "%d", &err_num);	
  printf("err_num=%d\n",err_num);
  fclose(fd);
  switch(err_num)
  {
    case 0:
      return TRUE;

    case 1:
      RUNNING = FALSE;
      sprintf(buffer,"%s","   Failed.   ");
      break;

    case 2:
      RUNNING = FALSE;
      sprintf(buffer,"%s","  Connected. ");
      break;

    case 3:
      RUNNING = FALSE;
      sprintf(buffer,"%s","  Time out. ");
      break;

    default:
      RUNNING = FALSE;
      sprintf(buffer,"%s","   Failed.   ");
      return FALSE;
  }	  
  g_object_set(lanlayout->bg->label,"text",buffer,NULL);
  g_object_set(lanlayout->bg->label,"fill-color","red",NULL);
  sms->busy = FALSE ;
	return FALSE;
}

static gboolean do_connect(Layout * layout)
{
  printf("~~~%s\n" ,__FUNCTION__);
  LanLayout *lanlayout = layout->private;
  pid_t pid;  
  if (signal(SIGCHLD, sig_action) == SIG_ERR)
  {
    printf ("error in signal!\n");
  }

  pid=fork();
  if(pid<0)
  {
    printf("fork error\n");
  }
  else if(pid==0)
  {
    execl("/etc/init.d/Snetwork", "Snetwork", "restart", (char *)0);
  }
  return FALSE;
}

static void lan_connect(Layout * layout)
{
  printf("~~~%s\n" ,__FUNCTION__);
  LanLayout *lanlayout = layout->private;
  remove(STATUS_FILE);
  DYNAMIC_CONNECT_RUN=TRUE;
  g_timeout_add(1000,(GSourceFunc)dynamic_connect_style,layout);
  RUNNING=TRUE;
  g_idle_add((GSourceFunc)do_connect, layout);
  g_idle_add((GSourceFunc)do_detect, layout);
}

static gboolean lan_dhcp_connect(Layout * layout)
{
  LanLayout *lanlayout = layout->private;

  memset(&(lanlayout->iface) , 0, sizeof(Iface_t));
  strcpy(lanlayout->iface.name , "eth0");
  printf("iface.name =%d\n" ,lanlayout->iface.name);
  lan_read_interface_info(&(lanlayout->iface));
  lanlayout->iface.isdhcp = TRUE;
  lan_wirte_interface_info(&(lanlayout->iface));
  lan_connect(layout);

  return TRUE ;
}

void get_name_server(const gchar *file, Layout * layout)
{
  LanLayout *lanlayout = layout->private;
  gchar *content;
  gchar **lines;
  gint length;
  gchar *delim;
  gint i = 0;
  GError *err = NULL;

  delim = g_strdup("\n");
  if (!g_file_get_contents(file, &content, &length, &err))
  {
    fprintf(stderr,"Could not access file: %s.\n",file);
    return;
  }
  lines = g_strsplit(content, delim, 2048);
  g_free(delim);
  delim = g_strdup("");
  g_free(content);

  gint k = 0;
  while (lines[i])
  {
    if ((g_strrstr(g_strchomp(lines[i]), "nameserver"))
        && (!g_str_has_prefix(g_strchomp(lines[i]), "#")))
    {
      delim = g_strrstr(lines[i],"=");
      if (delim)
        delim = g_strdup(g_strchomp(delim) + 1);
      if (!delim)
      {
        delim = g_strrstr(lines[i]," ");
        if (delim)
          delim = g_strdup(g_strchomp(delim) + 1);
      }
      lanlayout->iface.nameserver[k++] = g_strdup(delim);
    }
    i++ ;
  }
  g_strfreev(lines);
}

static void lan_set_staticip(Layout *layout)
{
	LanLayout *lanlayout = layout->private;
	SMS *sms = layout->sms ;
		GtkWidget *entry;
    gchar *name1 = NULL;
    gchar *name2 = NULL;
		gchar *newval = NULL;
    gchar *ipinfo = NULL;

    if (lanlayout->iface.nameserver[0] != NULL)
    {
        g_free(lanlayout->iface.nameserver[0]);
        lanlayout->iface.nameserver[0] = NULL;
    }
    if (lanlayout->iface.nameserver[1] != NULL)
    {
        g_free(lanlayout->iface.nameserver[1]);
        lanlayout->iface.nameserver[1] = NULL;
    }
    entry = g_object_get_data(G_OBJECT(sms->window), "Address");
    if (entry) 
    {
        newval = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
        printf("Address = %s\n" ,newval);
        if (lan_address_is_valid(newval))
        {
            strcpy(lanlayout->iface.address, newval);
        }
        else
        {
            ipinfo=g_strdup("Input Address is invalid");
            g_object_set(lanlayout->bg->label,"text",ipinfo,NULL);
            return;
        }
    }

    entry = g_object_get_data(G_OBJECT(sms->window), "Netmask");
    if (entry) 
    {
        newval = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
        printf("Netmask = %s\n" ,newval);
        if (lan_address_is_valid(newval))
        {
            strcpy(lanlayout->iface.netmask, newval);
        }
        else
        {
            ipinfo=g_strdup("Input Netmask is invalid");
            g_object_set(lanlayout->bg->label,"text",ipinfo,NULL);
            return;
        }
    }

    entry = g_object_get_data(G_OBJECT(sms->window), "gateway");
    if (entry)
    {
        newval = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
        printf("gateway = %s\n" ,newval);
        if (lan_address_is_valid(newval))
        {
            strcpy(lanlayout->iface.gateway, newval);
        }
        else
        {
            ipinfo=g_strdup("Input Gateway is invalid");
            g_object_set(lanlayout->bg->label,"text",ipinfo,NULL);
            return;
        }
    }

	entry = g_object_get_data(G_OBJECT(sms->window), "Primary DNS server");
	if (entry)
	{
					name1 = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
					printf("Primary DNS server = %s\n" ,name1);
        if (strlen(name1) >= 7)
        {
            if (lan_address_is_valid(name1))
            {
                lanlayout->iface.nameserver[0] = g_strdup(name1);
            }
            else
            {
            			g_object_set(lanlayout->bg->label,"nameserver option invalid",ipinfo,NULL);
                return;
            }
        }
	}

	entry = g_object_get_data(G_OBJECT(sms->window), "Secondary DNS server");
	if (entry)
	{
					name2 = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
					printf("Secondary DNS server = %s\n" ,name2);
        if (strlen(name2)>=7)
        {
            if (lan_address_is_valid(name2))
            {
                lanlayout->iface.nameserver[1] = g_strdup(name2);
            }
            else
            {
                printf("LAN::show nameserver option invalid 2: %s, len = %d\n", name2, strlen(name2));
                g_object_set(lanlayout->bg->label,"nameserver option invalid",ipinfo,NULL);       
                return;
            }
        }
	}

	lan_save_nameserver(layout);
	lan_wirte_interface_info(&(lanlayout->iface));
	printf("write interface info: isdhcp: %d, ip: %s, netmask: %s, gateway: %s, nameserver1: %s, nameserver2: %s\n", 
            lanlayout->iface.isdhcp, lanlayout->iface.address, lanlayout->iface.netmask, lanlayout->iface.gateway, 
            lanlayout->iface.nameserver[0], lanlayout->iface.nameserver[1]);
	do_connect(layout);
}

static gboolean lan_static_connect(Layout * layout)
{
  LanLayout *lanlayout = layout->private;

  memset(&(lanlayout->iface), 0, sizeof(Iface_t));
  strcpy(lanlayout->iface.name , "eth0");
  printf("---begain read interface info----\n");
  lan_read_interface_info(&(lanlayout->iface));
  lanlayout->iface.isdhcp = FALSE ;
  printf("lanlayout->iface.nameserver[0] =%p\n" ,lanlayout->iface.nameserver[0]);
  if ((lanlayout->iface.nameserver[0] == NULL) && (lanlayout->iface.nameserver[1] == NULL))
  {
    gchar *nameserverfile = NULL;
    nameserverfile = g_strdup("/etc/resolv.conf");
    get_name_server(nameserverfile, layout);
    if (!lanlayout->iface.nameserver[0]) 
      lanlayout->iface.nameserver[0] = g_strdup("");
    if (!lanlayout->iface.nameserver[1]) 
      lanlayout->iface.nameserver[1] = g_strdup("");					
  }
  lan_set_staticip(layout);

  return TRUE ;
}

static gboolean lan_address_is_valid(gchar *ip)
{
    struct sockaddr_in addr;

    if (ip == NULL)
    {
        return FALSE;
    }
    
    if (inet_pton(AF_INET, ip, &addr.sin_addr) == 0)
    {
        perror("inet_pton");
        printf("invalid: (%s), len: (%d)\n", ip, strlen(ip));
        return FALSE;
    }
    
    return TRUE;
}

static void lan_save_nameserver(Layout * layout)
{
		printf("~~~%s\n" ,__FUNCTION__);
    gchar *name1 = NULL;
    gchar *name2 = NULL;
    gboolean changed = FALSE;
    gboolean invalid = FALSE;

    LanLayout *lanlayout = layout->private;
    Entry *entry = lanlayout->entrys ;
    SMS *sms = layout->sms ;

	// save nameserver
    FILE *fd = fopen("/etc/resolv.conf", "w");     
    if (!fd)
    {
        printf("file /etc/resolv.conf cann't access\n");
    }
    if (lanlayout->iface.nameserver[0])
    {
        fprintf(fd, "nameserver %s\n", lanlayout->iface.nameserver[0]);
    }
    if (lanlayout->iface.nameserver[1])
    {
        fprintf(fd, "nameserver %s\n", lanlayout->iface.nameserver[1]);
    }
    fclose(fd);

    FILE *fd_bak = fopen("/etc/resolv.conf_bak", "w");        //add by zzw 2009.09.09
    if (!fd_bak)
    {
        printf("file /etc/resolv.conf cann't access\n");
    }	

    if (lanlayout->iface.nameserver[0])
    {
        fprintf(fd_bak, "nameserver %s\n", lanlayout->iface.nameserver[0]);
    }
    if (lanlayout->iface.nameserver[1])
    {
        fprintf(fd_bak, "nameserver %s\n", lanlayout->iface.nameserver[1]);
    }
    fclose(fd_bak);

    return;
    
}

static gboolean set_staticip(Layout * layout)
{
	printf("~~~%s\n" ,__FUNCTION__);
	//GtkWidget *entry_tmp ;
  gchar *name1 = NULL;
  gchar *name2 = NULL;
  gchar *newval = NULL;
  gchar *ipinfo = NULL;
  int i ;
  char buf[25] ;


  LanLayout *lanlayout = layout->private;
  Entry *entry = lanlayout->entrys ;
  SMS *sms = layout->sms ;

    if (lanlayout->iface.nameserver[0] != NULL)
    {
    			printf("~~~%s\n" ,__FUNCTION__);
        g_free(lanlayout->iface.nameserver[0]);
        lanlayout->iface.nameserver[0] = NULL;
    }
    if (lanlayout->iface.nameserver[1] != NULL)
    {
        g_free(lanlayout->iface.nameserver[1]);
        lanlayout->iface.nameserver[1] = NULL;
    }

    //entry = g_object_get_data(G_OBJECT(lanlayout->static_window), "address");
    for(i = 0 ; i < lanlayout->entry_count ; i++)
   	{
				entry->entry_lan = g_object_get_data(G_OBJECT(sms->window), entry->name);
				printf("~~~~~entry_lan = %s\n" ,entry->name);
				if (entry->entry_lan)
				{
						newval = gtk_editable_get_chars(GTK_EDITABLE(entry->entry_lan), 0, -1);
						printf("~~~%s\n" ,newval);
						if (lan_address_is_valid(newval))
						{
							printf("~~~%d\n",i);
							if(i == 0){
										strcpy(lanlayout->iface.address, newval);
							}
							else if(i == 1){
										strcpy(lanlayout->iface.netmask, newval);
							}
							else if(i == 2){
										strcpy(lanlayout->iface.gateway, newval);
							}
							else if(i == 3){
										printf("~~~%s\n" ,__FUNCTION__);
										strcpy(lanlayout->iface.nameserver[0], newval);
							}
							else if(i == 4){
										printf("~~~%s\n" ,__FUNCTION__);
										strcpy(lanlayout->iface.nameserver[1], newval);
							}
							
						}
						else 
						{
								printf("~~~!!!%d\n",i);
            	ipinfo=g_strdup_printf("Input %s is invalid" ,entry->name);
            	printf("~~%s\n" ,ipinfo);
    						g_object_set(lanlayout->bg->label,"text",ipinfo,NULL);
    						g_object_set(lanlayout->bg->label,"fill-color","red",NULL);			
            //show_message_box(lanlayout->static_window, ipinfo);//----display the adress is faild 
            return TRUE;							
						}
				}
				 if(i < lanlayout->entry_count-1){
								entry = entry->next ;
								printf("entry name =%s\n" ,entry->name);
				 	}
   }

    lan_save_nameserver(layout);
    lan_wirte_interface_info(&(lanlayout->iface));
    printf("write interface info: isdhcp: %d, ip: %s, netmask: %s, gateway: %s, nameserver1: %s, nameserver2: %s\n",
    	lanlayout->iface.isdhcp, lanlayout->iface.address, lanlayout->iface.netmask, lanlayout->iface.gateway, lanlayout->iface.nameserver[0], lanlayout->iface.nameserver[1]);
    do_connect(layout);
    printf("~~~~~set static\n");
			return TRUE;							
}

static gboolean lan_setting_layout_key_press(Layout * layout,GdkEventKey *event)
{
  LanLayout *lanlayout = layout->private;
  IconSet *icon;
  SMS *sms = layout->sms ;
  //Entry *entry = lanlayout->entrys ;
  int i;

  if(event->keyval == GDK_Up && lanlayout->selete_stat ==ICON_SELETE)
  {
    lan_setting_layout_move_icon(layout ,TRUE);
    //return TRUE;
  }
  else if(event->keyval == GDK_Down && lanlayout->selete_stat ==ICON_SELETE)
  {
    lan_setting_layout_move_icon(layout ,FALSE);
    //return TRUE;
  }
  else if(event->keyval == GDK_Escape)
  {		
    printf("esc!%d\n" ,LAYOUT_WIN_MAIN);
    hide_entrys(layout) ;
    sms_change_win_to(layout->sms,LAYOUT_WIN_SETTING);
    //return TRUE;
  }
  else if(event->keyval == GDK_Right)
  {
    lanlayout->selete_stat = WIDGET_SELETE ;
    printf("selete_stat = %d\n" ,lanlayout->selete_stat);
    //return TRUE;
  }
  else if(event->keyval == GDK_Left)
  {
    lanlayout->selete_stat = ICON_SELETE ;
    printf("selete_stat = %d\n" ,lanlayout->selete_stat);
    //return TRUE;
  }
  else if(event->keyval == GDK_Return && lanlayout->selete_stat ==ICON_SELETE)
  {
    printf("cur_icon_id = %d\n" ,lanlayout->focus_id);
    //DHCP select
    if(lanlayout->focus_id == 1)
    {
    		printf("!!!!~~~~DHCP\n");
      lanlayout->lan_stat = DHCP ;
      lan_dhcp_connect(layout);
    }
    //STATIC select
    else if (lanlayout->focus_id == 0)
    {
    		printf("!!!!~~~~STATIC\n");
      lanlayout->lan_stat = STATIC ;
      lan_static_connect(layout);
    }
#if 0
    else if(lanlayout->focus_id == 2)
    {
      lanlayout->lan_stat = COMFIRM ;
      lan_static_connect(layout);
      set_staticip(layout);
    }
#endif
  }
  
  printf("~~~~~~focus_id = %d\n" ,lanlayout->focus_id);
  if((lanlayout->focus_id == 1 || lanlayout->focus_id == 2)
      && lanlayout->selete_stat ==ICON_SELETE 
      && (event->keyval == GDK_Up || event->keyval == GDK_Down ))

  {
    Entry *entry = lanlayout->entrys ;
    printf("DHCP******\n");
    for(i = 0 ; i < lanlayout->entry_count ; i++)
    {
      printf("entry name = %s \n" ,entry->name);
      gtk_widget_set_sensitive(entry->entry_lan ,FALSE);
      entry = entry->next ;
    }
  }
  if(lanlayout->focus_id == 0 
      && lanlayout->selete_stat ==ICON_SELETE 
      && (event->keyval == GDK_Up || event->keyval == GDK_Down))

  {
    Entry *entry = lanlayout->entrys ;
    printf("STATIC******\n");
    for(i = 0 ; i < lanlayout->entry_count ; i++)
    {
      printf("entry name = %s \n" ,entry->name);
      gtk_widget_set_sensitive(entry->entry_lan ,TRUE);
      entry = entry->next ;
    }
  }
  return TRUE ;
}

Layout *lan_setting_layout_new(GooCanvasItem * parent,gpointer sms)
{
  Layout *layout=layout_new( parent,LAYOUT_TYPE_WIN ,LAYOUT_WIN_LAN_SETTING,
      LAN_SETTING_LAYOUT_NAME,sms);

  layout_set_func(layout,"init",lan_setting_layout_init);
  layout_set_func(layout,"clear",lan_setting_layout_clear);
  layout_set_func(layout,"key-press",lan_setting_layout_key_press);

  return layout;
}

